package com.pp.controller;


import com.alibaba.fastjson.JSONObject;
import com.pp.bean.PpUser;
import com.pp.bean.ResultPage;
import com.pp.bean.VoPage;
import com.pp.commne.Constant;
import com.pp.service.IPpImageService;
import com.pp.service.IPpUserService;
import com.pp.util.weixin.CheckWeChatToken;
import com.pp.util.weixin.WxpubOAuth;
import com.pp.util.weixin.bean.OAuthResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2019-03-06
 */
@Controller
@RequestMapping("/")
public class PpUserController {
    @Autowired
    IPpUserService ppUserService;
    @Autowired
    IPpImageService ppImageService;
    /**
     * 用户页面
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public Object login(String code, Model model) throws UnsupportedEncodingException {
            try {
                model.addAttribute("user", ppUserService.userInfo(code));
                //出现异常说明是刷新
            } catch (Exception e) {
                System.err.println("域名访问");
                return "redirect:"+WxpubOAuth.createOauthUrlForCode(Constant.APPID,Constant.RFESH_USER_INFO,true);
            }
        model.addAttribute("images",ppImageService.listByMap(new HashMap<>()));
        return "index";

    }

    /**
     *  表格查询
     * @param voPage
     * @return
     */
    @RequestMapping(value = "/findList", method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<PpUser> findList(VoPage voPage,PpUser ppUser) {
        return  ppUserService.findList(voPage,ppUser);
    }
    /**
     *  修改
     * @return
     */
    @RequestMapping(value = "/editPPuser", method = RequestMethod.POST)
    @ResponseBody
    public boolean editPPuser(PpUser ppUser) {
        if(ppUser.getVip()==1){
            ppUser.setAllowance(95+"");
        }     if(ppUser.getVip()==2){
            ppUser.setAllowance(9+"");
        }     if(ppUser.getVip()==3){
            ppUser.setAllowance(85+"");
        }
        return  ppUserService.updateById(ppUser);
    }

    //验证微信Token
    @RequestMapping("/checkWechatToken")
    public void checkWechatToken(String signature, String timestamp, String nonce, String echostr, HttpServletRequest request, HttpServletResponse response) {
        //字典排序 和 加密 后的token和微信服务器的token是否一样
        if (CheckWeChatToken.checkSignature(signature, timestamp, nonce)) {
            try {
                response.getWriter().print(echostr);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
